package com.wanxi.servlet.product;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.ProductVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/findAll")
public class ProductFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1。乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
//        String name = req.getParameter("name");
//        String normalPrice = req.getParameter("normalPrice");
//        String marketPrice = req.getParameter("marketPrice");
//        String enable = req.getParameter("enable");
//        String content = req.getParameter("content");
//        String cuisineId = req.getParameter("cuisineId");
//        String imgHref = req.getParameter("imgHref");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

//        3.封装到VO
        ProductVO productVO = ProductVO.builder()
//                .name(Tool.nullToStr(name))
//                .normalPrice(Tool.decimalToStr(normalPrice))
//                .marketPrice(Tool.decimalToStr(marketPrice))
//                .enable(Tool.strToInt(enable))
//                .content(Tool.nullToStr(content))
//                .cuisineId(Tool.strToInt(cuisineId))
//                .imgHref(Tool.nullToStr(imgHref))
                .page(Tool.strToPage(page))
                .limit(Tool.strToLimit(limit))
                .build();
//        4.调用服务逻辑层
        ProductService productService = new ProductServiceImpl();
//        5.得到逻辑层的返回结果
        ResultDTO resultDTO = productService.findAll(productVO);
//        6.将结果返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));

    }
}
